<template>
    <div :class="['work-record', activate?'selected':'']">
        <div class="work-record-info">
            <div class="work-record-date">{{ task.dead_line }}</div>
            <div class="work-record-title">{{ task.title }}</div>
            <div class="work-record-language">{{ task.course_name }}</div>
            <el-button v-if="userInfo.role == 2" class="deleteWord">删除</el-button> 
            <router-link v-if="userInfo.role == 2" :to="{path: '/user/creatework', params: {}}" class="show-btn">修改</router-link> 
            <div class="work-record-btn" @click="setActive">
                详情
                <i class="el-icon-caret-bottom"></i>
            </div>
        </div>
        <div class="work-record-detail">
            <div v-for="(item, index) in task.questions" :key="index" class="question-item">
                <div class="title" :style="{background: titleBg}">
                    题目{{ index + 1 }}
                </div>
                <div class="detail">
                    <div class="content">{{ item.text }}</div>
                    <router-link v-if="userInfo.role==2" :to="{ name: 'question', params: { questionId: item.ti_id, text: item.text, questionType: type, answer: item.answer}}"  class="show-btn">查看</router-link>
                    <router-link v-if="userInfo.role==1" :to="{ name: 'homework', params: { questionId: item.ti_id, text: item.text, questionType: type, projectId: item.project_id, courseId: task.course_id, answer: item.answer}}"  class="show-btn">开始编码</router-link>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import { getQuestions } from '@/utils/api'

var bg = require('../assets/题目标题.png')

export default {
  name: 'WorkRecord',
  props: {
    selected: {
      type: Boolean,
      default: false
    },
    task: {
      type: Object,
      default: function () {
        return {}
      }
    },
    type: {
      type: String,
      default: '1'
    }
  },
  computed: {
    userInfo () {
      return this.$store.state.userInfo
    }
  },
  data () {
    return {
      titleBg: 'url(' + bg + ') no-repeat',
      activate: this.selected,
      homework: {}
    }
  },
  methods: {
    async deleteWord () {
        console.log('删除准备中')
    },
    async setActive () {
      if (!this.task.questions) {
        let res = await getQuestions({ assignment_id: this.task.assignment_id })
        console.log(res)
        if (res.error === '10') {
          this.$store.commit('setQuestions', {task: this.task, questions: res.result})
          this.activate = !this.activate
        } else {
          this.$notify({
            title: '获取失败',
            message: res.result,
            type: 'error'
          })
        }
      } else {
        this.activate = !this.activate
      }
    }
  },
  mounted () {
    this.setActive()
    this.homework = {
        date: this.task.dead_line,
        title: this.task.title,
        courseName: this.task.course_name,

    }

  }
}
</script>
<style lang="less">
.work-record {
    border-bottom: 1px solid #bbb;
}

.work-record-info {
    display: flex;
    height: 40px;
    align-items: center;
    justify-content: space-between;
    color: #666;
}

.work-record-title {
    flex: 1;
    text-align: center;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    padding: 0 10px;
}

.work-record-language {
    flex: 1;
    text-align: center;
    padding: 0 10px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
}

.work-record-btn,
.show-btn {
    cursor: pointer;
}

.selected .work-record-btn {
    border: 1px solid #bbb;
    padding: 0 20px 5px 20px;
}

.work-record-detail-icon {
    display: none;
}

.selected .work-record-detail-icon {
    display: initial;
    font-size: 24px;
}

.work-record-detail {
    display: none;
    border: 1px solid #bbb;
    padding: 10px 30px 10px 20px;
}

.selected .work-record-detail {
    display: block;
}

.work-record-detail .question-item {
    margin-bottom: 20px;
}

.question-item .title {
    color: #fff;
    padding-left: 5px;
    width: 120px;
}

.question-item .detail {
    margin-left: 120px;
    background: #e5e5e5;
    padding: 15px 20px 15px 30px;
    display: flex;
    @media screen and (max-width: 768px){
        margin-left: 0
    }
}

.question-item .content {
    flex: 1;
    white-space:pre-wrap;
}

.question-item .show-btn {
    color: #2990cd;
    background: #fff;
    border: none;
    width: 120px;
    padding: 5px;
    align-self: flex-end;
    text-align: center;
}
</style>
